/**  
* @Title: CrUserService.java
* @Package com.lemon.service.cityRun
* @Description: (一句话阐述):
* @author Ran_cc
* @date 2018年10月18日
* @version V1.0  
*/
package com.lemon.service.cityRun;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.lemon.mapper.cityRun.CrUserAddressMapper;
import com.lemon.mapper.cityRun.CrUsersInfoMapper;
import com.lemon.model.cityRun.CrUserAddress;
import com.lemon.model.cityRun.CrUsersInfo;

/**
 * <p>Title: CrUserService</p>
 * <p>Description: 
 * </p>
 * <p>Company: </p>
 * @author Ran_cc
 * @date 2018年10月18日
 * @版本 V 1.0 
 */
@Service
public class CrUserServiceImpl implements CrUserService {

	@Resource
	private CrUsersInfoMapper uMapper;
	@Resource
	private CrUserAddressMapper addrMapper;

	/**
	 * <p>
	 * Title: selectListBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param user
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年10月18日
	 */
	@Override
	public List<CrUsersInfo> selectListBySelective(CrUsersInfo user) throws Exception {
		PageHelper.startPage(user.getPageNum() , user.getPageSize());
		List<CrUsersInfo> list = uMapper.selectListBySelective(user);
		return list;
	}

	/**
	 * <p>
	 * Title: updateUserInfoById
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param user
	 * @return
	 * @throws Exception
	 * @author Ran_cc
	 * @date 2018年10月18日
	 */
	@Override
	public int updateUserInfoById(CrUsersInfo user) throws Exception {
		int i = uMapper.updateByPrimaryKeySelective(user);
		return i;
	}

	/**
	 * <p>
	 * Title: queryOauthInfoByOpenId
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param openid
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年11月8日
	 */
	@Override
	public CrUsersInfo queryOauthInfoByOpenId(String openid) throws Exception {
		CrUsersInfo user = new CrUsersInfo();
		user.setWxOpenId(openid);
		CrUsersInfo usersInfo = uMapper.selectBySelective(user);
		return usersInfo;
	}

	/**
	 * <p>
	 * Title: insertUserInfo
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param tko
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年11月8日
	 */
	@Override
	public CrUsersInfo insertUserInfo(CrUsersInfo tko) throws Exception {
		int selective = uMapper.insertSelective(tko);
		return tko;
	}

	/**
	 * <p>
	 * Title: getUserAddressList
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param addr
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年11月9日
	 */
	@Override
	public List<CrUserAddress> getUserAddressList(CrUserAddress addr) throws Exception {
		PageHelper.startPage(addr.getPageNum() , addr.getPageSize());
		return addrMapper.selectListBySelective(addr);
	}

	/**
	 * <p>
	 * Title: addUserAddressList
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param addr
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年11月9日
	 */
	@Override
	public int addUserAddressList(CrUserAddress addr) throws Exception {
		CrUserAddress add = new CrUserAddress();
		add.setUserId(addr.getUserId());
		add.setWhere(" and a.status = 1 ");
		List<CrUserAddress> list = addrMapper.selectListBySelective(add);
		if (list.size() == 0) {
			addr.setIsDefault(1);
		}
		return addrMapper.insertSelective(addr);
	}

	/**
	 * <p>
	 * Title: updateUserAddressList
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param addr
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年11月9日
	 */
	@Override
	public int updateUserAddressList(CrUserAddress addr) throws Exception {

		return addrMapper.updateByPrimaryKeySelective(addr);
	}

	/**
	 * <p>
	 * Title: updateDefauleAddress
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param addr
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年11月9日
	 */
	@Transactional(rollbackFor = { Exception.class } , propagation = Propagation.REQUIRED)
	@Override
	public int updateDefauleAddress(CrUserAddress addr) throws Exception {
		//先查询出来当前为默认地址的地址
		CrUserAddress addr2 = new CrUserAddress();
		addr2.setIsDefault(1);
		addr2.setUserId(addr.getUserId());
		List<CrUserAddress> list = addrMapper.selectListBySelective(addr2);
		if (list.size() > 0) {
			//修改成不默认的
			CrUserAddress add = list.get(0);
			addr2 = new CrUserAddress();
			addr2.setIsDefault(0);
			addr2.setId(add.getId());
			addrMapper.updateByPrimaryKeySelective(addr2);
		}
		addr.setIsDefault(1);
		//在修改需要默认的
		return addrMapper.updateByPrimaryKeySelective(addr);
	}
}
